package com.ph.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ph.entity.MemberInfo;
import com.ph.shopping.facade.product.service.IPhProductClassifyService;
import com.ph.shopping.facade.product.service.IPhProductPropertyValService;
import com.ph.shopping.facade.product.service.IProductService;
import com.ph.shopping.facade.product.vo.PhProductClassifyVo;
import com.ph.shopping.facade.product.vo.PhProductPropertyValVo;
import com.ph.shopping.facade.product.vo.PhProductPropertyVo;
import com.ph.shopping.facade.product.vo.PhProductVo;
import com.ph.util.SessionUtil;

/**
 * @项目：phshopping
 * @描述： 商城index页面
 * @作者： xkw
 * @创建时间：2017/3/21
 * @Copyright by xkw
 * @version 1.0.0
 */
@Controller
@RequestMapping("/shopping/index")
public class ShoppingIndexController {

    /*日志记录器*/
//    private static final Logger logger = LoggerFactory.getLogger(ShoppingIndexController.class);

    /*商品接口*/
    @Reference(version = "1.0.0")
    private IProductService iProductService;
    /*商品类别接口*/
    @Reference(version = "1.0.0")
    private IPhProductClassifyService iPhProductClassifyService;
    
	//引用商品属性值service
	@Reference(version="1.0.0")
	private IPhProductPropertyValService iPhProductPropertyValService;
    /**
     * 商城首页数据加载
     * @author xkw
     * @param map
     * @return String
     * @throws Exception
     */
    @RequestMapping("/getIndexPage")
    public String getIndexPage(Map<String, Object> map,HttpServletRequest request,PhProductClassifyVo classifyVo ) throws Exception {
    	List<MemberInfo> list = new ArrayList<MemberInfo>();
    	list.add(SessionUtil.getAttribute(request)) ;
    	map.put("memberList", list);
    	classifyVo.setParentId(0L);
    	classifyVo.setDeleteFlag(1);
    	map.put("shoppingList", iPhProductClassifyService.getProductIndexList(classifyVo));
        return "index";
    }
    
    /**
     * 商城商品详情
     * @author hewl
     * @param Map<String, Object> map,PhProductVo phProductVo : id
     * @return String
     * @throws Exception
     */
    @RequestMapping("/getProductDateil")
    public String getProductDateil(HttpServletRequest request,Map<String, Object> map,PhProductVo phProductVo ) throws Exception {
    	PhProductClassifyVo classifyVo = new PhProductClassifyVo();
    	String id = String.valueOf(phProductVo.getId());
    	if(StringUtils.isNotEmpty(id)&&id!="null"){
	    	List<PhProductVo> list = iProductService.getProductVoDetail(phProductVo);//获取本商品信息
	    	if(list.size()>0){
	    		for(PhProductVo v:list){//单条取出本商品信息
	    			v.setPhProductClassifyVoList(null);
	    			PhProductVo productVo =new PhProductVo();
	    			productVo.setProductClassifyId(v.getProductClassifyId());
	    			productVo.setAuditState(3);
	    			productVo.setDeleteFlag(1);
	    			productVo.setSaleNoSaleStatus(1);
	    			List<PhProductVo> listVo = iProductService.getProductVoDetail(productVo);//查询同类别的商品
	    			if(listVo.size()>0){//判断是否存在相同商品的信息
	    				List<PhProductVo> listArrayVo =  new ArrayList<PhProductVo>();
	    				int i = 0;
	    				for(PhProductVo vo:listVo){//取出相同商品信息
	    					//如果是已有的则剔除
	    					if(String.valueOf(v.getId()).toString().equals(String.valueOf(vo.getId()).toString())){
	    						continue;
	    					}else{//没有的往里面加
	    						if(i<=3)
	    							listArrayVo.add(vo);
	    					}
	    					i++;
	    				}
	    				v.setPhProductVoList(listArrayVo);//塞值
	    			}
	    			if(v.getPhProductPropertyVoList().size()>0){
	    				for(PhProductPropertyVo propertyVo:v.getPhProductPropertyVoList()){
	    					PhProductPropertyValVo propertyValVo = new PhProductPropertyValVo();
	    					propertyValVo.setPropertyId(propertyVo.getId());
	    					propertyValVo.setProductId(v.getId());
	    					List<PhProductPropertyValVo> propertyValVoLst = iPhProductPropertyValService.getPhProductPropertyValVoList(propertyValVo);
	    					propertyVo.setPhProductPropertyValVoList(propertyValVoLst);
	    				}
	    			}
	    			classifyVo.setParentId(0L);
	    			classifyVo.setDeleteFlag(1);
	    			v.setPhProductClassifyVoList(iPhProductClassifyService.getProductClassifyVoAchildList(classifyVo));
	    		}
	    	}
	    	List<MemberInfo> memberInfoList = new ArrayList<MemberInfo>();
	    	memberInfoList.add(SessionUtil.getAttribute(request)) ;
	    	map.put("memberList", memberInfoList);
	    	map.put("shoppingPhProductData", list);
    	}else{
    		return "redirect:/";
    	}
    return "detail";
    }


}
